package com.fp.m7.client.widget;

import com.fp.m7.client.MyMessages;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.PopupPanel;

/**
 * Convenience class for displaying a 'are you sure' popup window. Used for deleting account.
 * 
 * @author 7
 */
public class ConfirmDialogBox extends DialogBox {

    private static MyMessages msg = GWT.create(MyMessages.class);

    private Button btnYes;

    private PopupPanel.PositionCallback centerPositionCallback = new PopupPanel.PositionCallback() {
        @Override
        public void setPosition(int offsetWidth, int offsetHeight) {
            int left = (Window.getClientWidth() - offsetWidth) / 2;
            int top = (Window.getClientHeight() - offsetHeight) / 2;
            setPopupPosition(left, top);
        }
    };

    public ConfirmDialogBox() {
        setHTML(msg.areYouSure());

        AbsolutePanel absolutePanel = new AbsolutePanel();
        setWidget(absolutePanel);
        absolutePanel.setSize("267px", "66px");

        btnYes = new Button(msg.ok());
        absolutePanel.add(btnYes, 10, 21);
        btnYes.setSize("120px", "24px");

        Button btnCancel = new Button(msg.cancel());
        absolutePanel.add(btnCancel, 136, 21);
        btnCancel.setSize("120px", "24px");
        btnCancel.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent paramClickEvent) {
                ConfirmDialogBox.this.hide();
            }
        });
    }

    public HasClickHandlers getConfirmButton() {
        return btnYes;
    }

    public void showCentered() {
        super.setPopupPositionAndShow(centerPositionCallback);
    }
}
